using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

public class PlayerMove : NetworkBehaviour
{
    [SerializeField] private float moveSpeed = 5f;  
    [SerializeField] private float rotateSpeed = 3f; 
    [SerializeField] private float spawnRange = 8f;

    public override void OnNetworkSpawn()
    {
        if (IsOwner)
        {
            InitPlayerDataServerRpc();
        }
    }

    void Update()
    {
        if (!IsOwner) return;
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 moveDirection = new Vector3(horizontal, 0f, vertical).normalized;
        if (moveDirection.sqrMagnitude > 0.1f)  // 避免零输入时的误差
        {
            transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
            Quaternion targetRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
        }
    }

    [ServerRpc]
    private void InitPlayerDataServerRpc()
    {
        transform.position = new Vector3(Random.Range(-spawnRange, spawnRange), 0, Random.Range(-spawnRange, spawnRange));
        transform.rotation = new Quaternion(0, Random.Range(0, 360), 0, 0);
    }
}

